package cn.jhc.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import cn.jhc.bean.Author;
import cn.jhc.bean.Publisher;
import cn.jhc.bean.Title;
import cn.jhc.factory.ConnectionFactory;

public class AuthorDAO {
	
	public List<Author> getAll(){
		List<Author> list = new ArrayList<>();
		Connection connection = ConnectionFactory.getConnection();
		String sql = "select authorid,firstname,lastname from authors";
		Statement statement = null;
		ResultSet rs = null;

		try {
			statement = connection.createStatement();
			rs = statement.executeQuery(sql);
			while (rs.next()) {
				Author authors = new Author();
				authors.setAuthorID(rs.getInt(1));
				authors.setFirstName(rs.getString(2));
				authors.setLastName(rs.getString(3));
				list.add(authors);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				rs.close();
				statement.close();
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return list;
	}
	
	public void addAuthors(Author authors) {
		Connection connection = ConnectionFactory.getConnection();
		PreparedStatement ps = null;
		Statement statement = null;
		ResultSet rs = null;
		String a = authors.getFirstName();
		String b = authors.getLastName();
		String sql = "insert into authors(firstname,lastname) value(a,b)";
		try {
			statement = connection.createStatement();
			rs = statement.executeQuery(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			try {
				statement.close();
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public void deleteAuthors(int id) {
		Connection connection = ConnectionFactory.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "delete from authors where authorID=?";
		try {
			ps = connection.prepareStatement(sql);
			ps.setInt(1, id);
			ps.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			try {
				ps.close();
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	
	public Author findByName(String name) {
		Author authors = new Author();
		Connection connection = ConnectionFactory.getConnection();
		Statement stat= null;
		ResultSet rs = null;
		String sql = "select authorid,firstname,lastname from authors where firstname+lastname="+name;
		try {
			stat = connection.createStatement();
			rs = stat.executeQuery(sql);
			while(rs.next()){
				authors.setAuthorID(rs.getInt(1));
				authors.setFirstName(rs.getString(2));
				authors.setLastName(rs.getString(3));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			try {
				stat.close();
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return authors;
	}
}
